package com.ngc.mall.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.exception.BusinessException;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.core.enumeration.ActivityStatus;
import com.ngc.mall.domain.LotteryDomain;
import com.ngc.mall.domain.LotteryItemDomain;
import com.ngc.mall.domain.LotteryPrizeDomain;
import com.ngc.mall.dto.LotteryDTO;
import com.ngc.mall.dto.LotteryItemDTO;
import com.ngc.mall.mapper.LotteryMapper;
import com.ngc.mall.service.ILotteryItemService;
import com.ngc.mall.service.ILotteryPrizeService;
import com.ngc.mall.service.ILotteryService;
import com.ngc.mall.vo.LotteryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 抽奖大转盘实现类
 * @author: chenxiaolin
 * @date 2023/10/14 14:05
**/
@Service
public class LotteryServiceImpl extends BaseServiceImpl<LotteryMapper, LotteryDomain> implements ILotteryService {

    @Autowired
    private ILotteryItemService iLotteryItemService;

    @Autowired
    private ILotteryPrizeService iLotteryPrizeService;

    @Override
    @Transactional
    public Long create(LotteryDTO dto){
        LocalDateTime now=LocalDateTime.now();
        int status=checkStatus(now,dto.getStartDate(),dto.getEndDate());
       Long lotteryId=super.create(LotteryDomain.builder().bgImg(dto.getBgImg()).startDate(dto.getStartDate()).endDate(dto.getEndDate()).img(dto.getImg()).rule(dto.getRule()).lotteryType(dto.getLotteryType()).topic(dto.getTopic()).integral(dto.getIntegral()).status(status).build());
       List<LotteryItemDomain> lotteryItemDomainList=new ArrayList<>();
       for(LotteryItemDTO lotteryItemDTO:dto.getLotteryItemList()){
           LotteryPrizeDomain prizeDomain=lotteryItemDTO.getPrize();
           Long lotteryPrizeId;
           if(prizeDomain!=null){
               prizeDomain.setLotteryId(lotteryId);
               lotteryPrizeId=iLotteryPrizeService.create(lotteryItemDTO.getPrize());
           }else{
               lotteryPrizeId=-1L;
           }
           lotteryItemDomainList.add(LotteryItemDomain.builder().lotteryId(lotteryId).itemName(lotteryItemDTO.getItemName()).type(lotteryItemDTO.getType()).percent(lotteryItemDTO.getPercent()).prizeId(lotteryPrizeId).build());
       }
       iLotteryItemService.saveBatch(lotteryItemDomainList);
       return lotteryId;
    }

    @Override
    public List<LotteryDomain> selectAll(){
        List<LotteryDomain> lotteryDomainList= super.list(Wrappers.<LotteryDomain>lambdaQuery().orderByDesc(LotteryDomain::getCreateTime));
        List<LotteryDomain> lotteryList=new ArrayList<>();
        LocalDateTime now=LocalDateTime.now();
        for(LotteryDomain lotteryDomain:lotteryDomainList){
            int status=checkStatus(lotteryDomain.getStartDate(),lotteryDomain.getEndDate(),now);
            if(status!=lotteryDomain.getStatus()){
                lotteryDomain.setStatus(status);
                super.updateById(lotteryDomain);
            }
            lotteryList.add(lotteryDomain);
        }
        return lotteryList;
    }

    public int checkStatus(LocalDateTime now,LocalDateTime start,LocalDateTime end){
        if (now.isAfter(start) && now.isBefore(end)) {
          return ActivityStatus.DOING.getValue();
        } else if (now.isBefore(start)) {
            return ActivityStatus.PENDING.getValue();
        } else if (now.isAfter(end)) {
            return ActivityStatus.COMPLETED.getValue();
        } else {
            throw BusinessException.FAILURE;
        }
    }

    @Override
    public LotteryVO findById(Long id){
        LotteryDomain domain=super.check(id);
        LocalDateTime now=LocalDateTime.now();
        ///不获取奖项对应的奖品详情，点击进奖项再去获取
        List<LotteryItemDomain> lotteryItemList= iLotteryItemService.list(Wrappers.<LotteryItemDomain>lambdaQuery().eq(LotteryItemDomain::getLotteryId,domain.getId()));
        int status=checkStatus(now,domain.getStartDate(),domain.getEndDate());
        LotteryVO vo=LotteryVO.builder().bgImg(domain.getBgImg()).endDate(domain.getEndDate()).startDate(domain.getStartDate()).status(status).img(domain.getImg()).topic(domain.getTopic()).id(domain.getId()).integral(domain.getIntegral()).lotteryType(domain.getIntegral()).rule(domain.getRule()).lotteryItemList(lotteryItemList).build();
        return vo;
    }

    @Override
    public Boolean updateById(Long id,LotteryDomain lotteryDomain){
        LotteryDomain domain=super.check(id);
        lotteryDomain.setId(id);
        LocalDateTime now=LocalDateTime.now();
        int status=checkStatus(now,domain.getStartDate(),domain.getEndDate());
        if(domain.getStatus()!=status){
            lotteryDomain.setStatus(status);
        }
        ///更新活动基本信息
        return super.updateById(lotteryDomain);
    }

    @Override
    @Transactional
    public Boolean deleteById(Long id){
        super.removeById(id);
        iLotteryItemService.remove(Wrappers.<LotteryItemDomain>lambdaQuery().eq(LotteryItemDomain::getLotteryId,id));
        return iLotteryPrizeService.remove(Wrappers.<LotteryPrizeDomain>lambdaQuery().eq(LotteryPrizeDomain::getLotteryId,id));
    }



}
